`timescale  1ns/1ns
module tb_coordinate_gen;

localparam image_width  = 1024;
localparam image_height = 600;
//----------------------------------------------------------------------
//  clk & rst_n
reg                             clk;
reg                             rst_n;

initial
begin
    clk = 1'b0;
    forever #5 clk = ~clk;
end

initial
begin
    rst_n = 1'b0;
    repeat(50) @(posedge clk);
    rst_n = 1'b1;
end

//----------------------------------------------------------------------
//  Image data prepred to be processed
reg                             per_img_vsync;
reg                             per_img_href;
reg                             per_img_valid;
wire                            post_img_vsync;
wire                            post_img_href ;
wire                            post_img_valid; 
reg             [7:0]           per_img_red;
reg             [7:0]           per_img_green;
reg             [7:0]           per_img_blue;

//  Image data has been processed
wire            [10:0]          pixel_xpos;
wire            [10:0]          pixel_ypos;

//----------------------------------------------------------------------
//  task and function
task image_input;
    bit             [31:0]      row_cnt;
    bit             [31:0]      col_cnt;
    bit             [7:0]       mem     [image_width*image_height*3-1:0];
    $readmemh("D:/Matlab/Matlab_Projects/2_Matlab_Project/2_VIP_RGB888_to_YCbCr444/img_RGB.dat",mem);
    
    for(row_cnt = 0;row_cnt < image_height-1;row_cnt++)
    begin
        repeat(5) @(posedge clk);
        per_img_vsync = 1'b0;
        repeat(5) @(posedge clk);
        for(col_cnt = 0;col_cnt < image_width-1;col_cnt++)
        begin
            per_img_valid = ~per_img_valid;
            per_img_href  = 1'b1;
            per_img_red   = mem[(row_cnt*image_width+col_cnt)*3+0];
            per_img_green = mem[(row_cnt*image_width+col_cnt)*3+1];
            per_img_blue  = mem[(row_cnt*image_width+col_cnt)*3+2];
            @(posedge clk);
        end
        per_img_href  = 1'b0;
    end
    per_img_vsync = 1'b1;
    @(posedge clk);
    
endtask : image_input

coordinate_gen  u_coordinate_gen (
    .clk                     ( clk                      ),
    .rst_n                   ( rst_n                    ),
    .per_frame_vsync         ( per_img_vsync         ),
    .per_frame_href          ( per_img_href          ),
    .per_frame_valid         ( 1'd1         ),
    .post_frame_vsync        (post_img_vsync),
    .post_frame_href         (post_img_href ),
    .post_frame_valid        (post_img_valid),    

    .pixel_xpos              ( pixel_xpos        ),
    .pixel_ypos              ( pixel_ypos        )
);

initial
begin
    per_img_vsync = 0;
    per_img_href  = 0;
    per_img_valid = 0;
    per_img_red   = 0;
    per_img_green = 0;
    per_img_blue  = 0;
end

initial 
begin
    wait(rst_n);
    fork
        begin 
            repeat(5) @(posedge clk); 
            repeat(3) begin
                image_input;
                repeat(1000) @(posedge clk);
            end
            $stop;
        end 
    join
end 

endmodule